package Day08_11;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test_Genericity {
    public static void main(String[] args) {
        List<Animal> myList = new ArrayList<>();
        Animal a = new Animal();
        Bird b = new Bird();
        Cat c = new Cat();

        myList.add(a);
        myList.add(b);
        myList.add(c);
        Iterator<Animal> it = myList.iterator();

        while(it.hasNext()){
            Animal A = it.next();
            if(A instanceof Bird){
               // ((Bird) it.next()).fly();
                Bird x = (Bird)A;
                x.fly();
            }
            else if(A instanceof Cat){
                //((Cat) it.next()).catchMouse();
                Cat y = (Cat)A;
                y.catchMouse();
            }
            else
                A.move();

//            if(it.next() instanceof Bird){  //it.next 存放的是地址  指向
//                ((Bird) it.next()).fly();
//            }
//            if(it.next() instanceof Cat){
//                ((Cat) it.next()).catchMouse();
//            }
        }
    }
}
class Animal{
    public void move(){
        System.out.println("动物在移动......");
    }
}
class Bird extends Animal{
    public void fly(){
        System.out.println("鸟儿在飞翔..........");
    }
}
class Cat extends Animal{
    public void catchMouse(){
        System.out.println("猫在抓老鼠.......");
    }
}
